﻿using FarmSmartSleep.Common.Domain.RBACFile;
using MediatR;
using Microsoft.EntityFrameworkCore;
using SheepCultivation.Common.Dto;
using SheepCultivation.Common.Infrastructure.Interface;
using SheepCultivation.Enum;

namespace SheepCultivation.API.AddliCation.RBAC
{
    public class EmployeeListHandler : IRequestHandler<EmployeeListCommon, Result>
    {
        public readonly IBaseRepository<Employee> employeeRepository;
        public EmployeeListHandler(IBaseRepository<Employee> employeeRepository)
        {
            this.employeeRepository = employeeRepository;
        }
        public async Task<Result> Handle(EmployeeListCommon request, CancellationToken cancellationToken)
        {
            var res = new Result() { Code = (int)CommonEnumCode.error };
            try
            {
                res.Code = (int)CommonEnumCode.success;
                var data = new pageALL();
                var list = employeeRepository.GetAll().ToList();
                if (!string.IsNullOrEmpty(request.FirstName))
                {
                    list = list.Where(c => c.FirstName == request.FirstName).ToList();
                }
                if (request.Department > 0)
                {
                    list = list.Where(c => c.Department == request.Department).ToList();
                }
                if (request.EmploymentStatus > 0)
                {
                    list = list.Where(c => c.EmploymentStatus == request.EmploymentStatus).ToList();
                }
                data.totalCount = list.Count();
                data.totalPage = (int)Math.Ceiling(list.Count() / (double)request.pageSize);
                list = list.Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();
                res.Code = (int)CommonEnumCode.success;
                res.Data = list;
                res.Msg = "员工显示";


            }
            catch (Exception)
            {
                throw;
            }
            return res;
        }
    }

    public class pageALL()
    {
        public int totalCount { get; set; }
        public int totalPage {  get; set; }
    }
}
